package com.hengpeng.itfinoss.service.fund.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinbase.persist.Transfer;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.fund.TransferService;

/**
 * 商户接口实现
 */

@Service
@Lazy
public class TransferServiceImpl implements TransferService
{

	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<List<Transfer>> findWithPage(String outTrustAccountId, String inTrustAccountId,
			Integer pageIndex, Integer pageSize, LinkedHashMap<String, OrderType> sortMap)
	{
		DynamicQuery query = new DynamicQuery(Transfer.class);
		if (!StringUtils.isEmpty(outTrustAccountId))
		{
			query.eq("outTrustAccountId", outTrustAccountId);
		}
		if (!StringUtils.isEmpty(inTrustAccountId))
		{
			query.eq("inTrustAccountId", inTrustAccountId);
		}
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					query.desc(entry.getKey());
				}
				else
				{
					query.asc(entry.getKey());
				}
			}
		}
		else
		{
			query.asc("id");
		}
		return new ServiceResult<List<Transfer>>(serviceFactory.getPersistService().find(Transfer.class, query));
	}

	@Override
	public ServiceResult<Long> queryCount(String outTrustAccountId, String inTrustAccountId)
	{
		DynamicQuery query = new DynamicQuery(Transfer.class);
		if (!StringUtils.isEmpty(outTrustAccountId))
		{
			query.eq("outTrustAccountId", outTrustAccountId);
		}
		if (!StringUtils.isEmpty(inTrustAccountId))
		{
			query.eq("inTrustAccountId", inTrustAccountId);
		}
		return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(query, null));
	}
}
